package com.yunmaozj.easydb.annotions.factory;

import com.yunmaozj.easydb.annotions.Column;
import com.yunmaozj.easydb.annotions.EasyDB;
import com.yunmaozj.easydb.common.StringUtil;
import com.yunmaozj.easydb.sql.SQL;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhongminghong
 * @email yunmaozj@qq.com
 * @create 2018-05-28 11:27
 */
public abstract class AbstractDBAnnotionsFactory {

    private EasyDB easyDB;
    private Connection connection;
    private SQL sql;

    public AbstractDBAnnotionsFactory(EasyDB easyDB, Connection connection) {
        this.easyDB = easyDB;
        this.connection = connection;
        this.sql = new SQL();
    }

    public String[] getColumns(){
        Class clazz = easyDB.value();
        List<String> cloumnList = new ArrayList<>();
        while (!Object.class.equals(clazz)){
            Field[] fields = clazz.getDeclaredFields();
            for(Field field : fields){
                Column column = field.getAnnotation(Column.class);
                if(column != null){
                    cloumnList.add(StringUtil.isEmpty(column.value()) ? field.getName() : column.value());
                }
            }
            clazz = clazz.getSuperclass();
        }
        String[] columns = new String[cloumnList.size()];
        return cloumnList.toArray(columns);
    }
}
